import { Controller } from "../../mvc/core/Controller";
import { IView } from "../../mvc/interfaces/IView";
import { BattleModule } from "../../../modules/BattleModule";
import { BattleQieCuoHintIcon } from "../view/battleQieCuo/BattleQieCuoHintIcon";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { ServerCommand } from "../mvc/ServerCommand";
import { DataEvent } from "../events/DataEvent";
import { PlayerMiniInfo } from "../model/PlayerMiniInfo";
import { GameProxy } from "../mvc/GameProxy";
import { SBattleReply } from "../../../Message/Public/SBattleReply";
import { SBattleApply } from "../../../Message/Public/SBattleApply";
import { RolePlayer } from "../scene/player/entity/RolePlayer";
import { MsgManager } from "../manager/MsgManager";
import { Language } from "../../../extend/language/Language";
import { SecTimer } from "../../../com/gengine/core/frame/SecTimer";
import { TimerType } from "../../../com/gengine/core/frame/TimerType";
import { UiHintIconView } from "../view/uiIconBtn/UiHintIconView";
import { Alert } from "../../../com/mui/controls/Alert";
import { TimerAlertWin } from "../view/common/alertwins/TimerAlertWin";
import { SBattleResult } from "../../../Message/Public/SBattleResult";
import { IBattleModule } from "../../../modules/interfaces/IBattleModule";
import { SBattleStart } from "../../../Message/Public/SBattleStart";
type int = number;
//class BattleController
    
    export  class BattleController extends Controller
    {
       

        protected /* override */ initView(): IView
        {
            if (!this._battleModule) 
            {
                this._battleModule = new BattleModule();
            }
            return this._battleModule;
        }

        public get hintIcon(): BattleQieCuoHintIcon
        {
            if (!this._battleHintIcon) 
            {
                this._battleHintIcon = new BattleQieCuoHintIcon();
            }
            return this._battleHintIcon;
        }

        protected /* override */ initServer(): void
        {
            Dispatcher.addEventListener(EventName.BattleApply, this.onBattleApply);
            Dispatcher.addEventListener(EventName.BattleReply, this.onBattleReply);
            NetDispatcher.addCmdListener(ServerCommand.BattleApplyCommand, this.BattleApplyHandler);
            NetDispatcher.addCmdListener(ServerCommand.BattleResultCommand, this.BattleResultHandler);
            NetDispatcher.addCmdListener(ServerCommand.BattleStartCommand, this.BattleStartHandler);
            return;
        }

        /* internal  */onBattleApply(arg1: DataEvent): void
        {
            var loc1=arg1.data as PlayerMiniInfo;
            GameProxy.battle.applyBattle(loc1.entityId);
            return;
        }

        /* internal  */onBattleReply(arg1: DataEvent): void
        {
            var loc1=arg1.data as SBattleReply;
            GameProxy.battle.replyBattle(loc1.toEntityId, loc1.isAgree);
            return;
        }

        /* internal  */BattleApplyHandler(arg1: SBattleApply): void
        {
            if (RolePlayer.instance.entityInfo.isStall) 
            {
                MsgManager.showRollTipsMsg(Language.getString(42100));
                return;
            }
            if (RolePlayer.instance.isFishing) 
            {
                MsgManager.showRollTipsMsg(Language.getString(42101));
                return;
            }
            if (RolePlayer.instance.isDoubleRest) 
            {
                MsgManager.showRollTipsMsg(Language.getString(42102));
                return;
            }
            this._curSBattleApple = arg1;
            if (!this._secTimer) 
            {
                this._secTimer = new SecTimer(1, 30);
                this._secTimer.addListener(TimerType.COMPLETE, this.onTimerCompleteHandler);
            }
            this._secTimer.reset();
            this._secTimer.start();
            this.showHint();
            return;
        }

        /* internal  */onTimerCompleteHandler(arg1: SecTimer=null): void
        {
            UiHintIconView.instance.removeHintIcon(this._battleHintIcon);
            this._secTimer.dispose();
            this._secTimer = null;
            return;
        }

        /* internal  */showHint(): void
        {
            this.hintIcon.addEventListener(flash.events.MouseEvent.CLICK, this.onBattleHintClick);
            UiHintIconView.instance.addHintIcon(this.hintIcon);
            this.hintIcon.showHint();
            return;
        }

        protected onBattleHintClick(arg1: Object /* flash.events.MouseEvent */): void
        {
            var event: Object /* flash.events.MouseEvent */;
            var onChoose: Function;

            var loc1;
            onChoose = null;
            event = arg1;
            onChoose = (arg1: int): void
            {
                var loc1=new SBattleReply();
                loc1.toEntityId = _curSBattleApple.playerInfo.entityId;
                if (arg1 != Alert.OK) 
                {
                    if (arg1 == Alert.CANCEL) 
                    {
                        loc1.isAgree = false;
                        Dispatcher.dispatchEvent(new DataEvent(EventName.BattleReply, loc1));
                    }
                }
                else 
                {
                    loc1.isAgree = true;
                    Dispatcher.dispatchEvent(new DataEvent(EventName.BattleReply, loc1));
                }
                return;
            }
            Alert.timerNO = true;
            Alert.mode = Alert.Mode_NoneNotModal;
            Alert.alertWinRenderer = TimerAlertWin;
            Alert.buttonWidth = 80;
            Alert.timerOut = this._secTimer.repeatCount;
            Alert.okLabel = Language.getString(42103);
            Alert.calcelLabel = Language.getString(42104);
            Alert.show(Language.getStringByParam(42105, this._curSBattleApple.playerInfo.name), Language.getString(42106), Alert.OK | Alert.CANCEL, null, onChoose);
            this.onTimerCompleteHandler();
            return;
        }

        /* internal  */BattleResultHandler(arg1: SBattleResult): void
        {
            (view as IBattleModule).battleResult(arg1);
            return;
        }

        /* internal  */BattleStartHandler(arg1: SBattleStart): void
        {
            (view as IBattleModule).battleStart(arg1);
            return;
        }

        public isInitView(): Boolean
        {
            return !(this._battleModule == null);
        }

        private /* var */_battleModule: IBattleModule;

        private /* var */_battleHintIcon: BattleQieCuoHintIcon;

        private /* var */_secTimer: SecTimer;

        private /* var */_curSBattleApple: SBattleApply;
    }
